package com.cqc.paoclient.ui.mime.myarticle;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.cqc.paoclient.R;
import com.cqc.paoclient.adapter.ArticleRvAdapter;
import com.cqc.paoclient.base.BaseActivity;
import com.cqc.paoclient.model.ArticleModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by cui on 2017/11/26.
 * 我的文章
 */

public class MyArticleActivity extends BaseActivity<MyArticlePresenter> implements MyArticleContract.View, OnRefreshListener, OnLoadmoreListener {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private List<ArticleModel.ItemsBean> articleList = new ArrayList<>();
    private ArticleRvAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_article;
    }

    @Override
    public MyArticlePresenter getmPresenter() {
        return new MyArticlePresenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null) {
            mPresenter.setView(this);
        }
    }

    @Override
    public void initView() {
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadmoreListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
    }

    @Override
    public void initData() {
        adapter = new ArticleRvAdapter(articleList);
        recyclerView.setAdapter(adapter);
        refreshLayout.autoRefresh();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }

    @Override
    public void getMyArticleSuccess(ArticleModel articleModel) {
        stopRefresh(refreshLayout);
        articleList.clear();
        articleList.addAll(articleModel.getItems());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getMyArticleFail(String error) {
        stopRefresh(refreshLayout);
        stopLoadMore(refreshLayout);
        showShort(error);
        tvNoData.setVisibility(View.VISIBLE);
    }


    @Override
    public void loadMoreMyArticleSuccess(ArticleModel articleModel) {
        stopLoadMore(refreshLayout);
        articleList.addAll(articleModel.getItems());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPresenter.getMyArticle();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mPresenter.loadMoreMyArticle();
    }
}
